package com.roc.pan.html;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.roc.pan.config.ConfigUtils;
import com.roc.pan.html.response.StepOneResponse;
import com.roc.pan.html.response.StepTwoResponse;
import com.sky.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author roc
 * @date 2023/2/8
 */
@Slf4j
public class HtmlContentUtils {

    private static final String URL_PREFIX = "http://ct.vpan123.com/";

    private List<String> result = Lists.newArrayList();


    public void parseHtml() throws IOException {
        String index = "d/27041266.html";
        Document parse = Jsoup.connect(URL_PREFIX + index).get();
        Elements elementsByClass = parse.getElementsByClass("folder-close");
        for (Element element : elementsByClass) {
            if (element.text().equals("世界近现代史") || element.text().equals("中国近现代史")) {
                parseSubHtml(element.getElementsByTag("a").attr("href"));
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String url : result) {
            String dl = parseCtPan(url);
            stringBuilder.append(dl + "\n");
        }
        fileWriterMethod(stringBuilder.toString());
    }

    private void parseSubHtml(String baseUrl) throws IOException {
        Document parse = Jsoup.connect(URL_PREFIX + baseUrl).get();
        Elements elementsByClass = parse.getElementsByClass("music");
        for (Element element : elementsByClass) {
            parseDownloadUrl(element.getElementsByTag("a").attr("href"));
        }
    }

    private void parseDownloadUrl(String url) throws IOException {
        Document parse = Jsoup.connect(URL_PREFIX + url).get();
        Elements elementsByClass = parse.getElementsByClass("pt");
        for (Element element : elementsByClass) {
            parseFinalUrl(element.getElementsByTag("a").attr("href"));
        }
    }

    private void parseFinalUrl(String url) throws IOException {
        Document parse = Jsoup.connect(URL_PREFIX + url).get();
        Elements elementsByClass = parse.getElementsByTag("a");
        for (Element element : elementsByClass) {
            if (element.text().equals("下载地址1")) {
                log.info(element.attr("href"));
                result.add(element.attr("href"));
            }
        }
    }

    public String parseCtPan(String url) {
        String file = url.substring(url.lastIndexOf("/") + 1);
        String[] fileArray = file.split("-");
        String token = "70vk46k1dkjf2picokvvfg";
        String url1 = "https://webapi.ctfile.com/getfile.php";
        HttpUtils httpUtils = new HttpUtils();
        Map<String, String> params = Maps.newHashMap();
        params.put("path", "file");
        params.put("f", file);
        params.put("token", token);
        params.put("ref", "https://ctfile.qinlili.bid");
        params.put("passcode", "");
        params.put("r", "0.6861165707896555");
        StepOneResponse stepOneResponse = null;
        try {
            stepOneResponse = httpUtils.get(url1, params, StepOneResponse.class);
            String url2 = "https://webapi.ctfile.com/get_file_url.php";
            Map<String, String> params2 = Maps.newHashMap();
            params2.put("uid", fileArray[0]);
            params2.put("fid", fileArray[1]);
            params2.put("file_chk", stepOneResponse.getFile().getFileChk());
            params2.put("app", "0");
            params2.put("acheck", "2");
            params2.put("r", "0.6861165707896555");
            StepTwoResponse stepTwoResponse = httpUtils.get(url2, params2, StepTwoResponse.class);
            if (stepTwoResponse.getCode().equals(200)) {
                return stepTwoResponse.getDownurl();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public void fileWriterMethod(String content) throws IOException {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(ConfigUtils.getConfig().getDownloadResource());
            fileWriter.append(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileWriter.close();
        }
    }

    public static void main(String[] args) throws IOException {
        HtmlContentUtils htmlContentUtils = new HtmlContentUtils();
        htmlContentUtils.parseHtml();
        //log.info(htmlContentUtils.parseCtPan("https://474b.com/file/14928255-237361302"));
    }
}
